using System;
using UnityEngine;

namespace NodeCanvas.DialogueTrees
{
	[AddComponentMenu("")]
	[Name("Action")]
	[Description("Execute an Action Task for the Dialogue Actor selected. The Blackboard will be taken from the selected Actor.")]
	public class DLGActionNode : DLGNodeBase, ITaskAssignable<ActionTask>, ITaskAssignable
	{
		[SerializeField]
		private UnityEngine.Object _action;

		[HideInInspector]
		public Task task
		{
			get
			{
				return action;
			}
			set
			{
				action = (ActionTask)value;
			}
		}

		public UnityEngine.Object serializedTask
		{
			get
			{
				return _action;
			}
		}

		private ActionTask action
		{
			get
			{
				return _action as ActionTask;
			}
			set
			{
				_action = value;
				if (value != null)
				{
					value.SetOwnerSystem(this);
				}
			}
		}

		public override string nodeName
		{
			get
			{
				return base.nodeName + " " + base.finalActorName;
			}
		}

		protected override Status OnExecute()
		{
			if (!base.finalActor)
			{
				base.DLGTree.StopGraph();
				return Error("Actor not found");
			}
			if (!action)
			{
				OnActionEnd(true);
				return Status.Success;
			}
			base.DLGTree.currentNode = this;
			base.status = Status.Running;
			action.ExecuteAction(base.finalActor, base.finalBlackboard, OnActionEnd);
			return base.status;
		}

		private void OnActionEnd(ValueType success)
		{
			if ((bool)(object)success)
			{
				Continue();
				return;
			}
			base.status = Status.Failure;
			base.DLGTree.StopGraph();
		}

		protected override void OnReset()
		{
			if ((bool)action)
			{
				action.EndAction(false);
			}
		}

		public override void OnGraphPaused()
		{
			if ((bool)action)
			{
				action.PauseAction();
			}
		}
	}
}
